import 'package:intl/intl.dart';

class PriceUtil {
  static String getOrderSumPrice(double? price) {
    print("getOrderSumPrice $price");
    if (price == null) {
      return '0';
    }
    NumberFormat format = new NumberFormat("#,###.##");
    return format.format(price);
  }

  static String formatNum(double num,int postion){
    if((num.toString().length-num.toString().lastIndexOf(".")-1)<postion){
      return num.toString().substring(0,num.toString().lastIndexOf(".")+postion+1).toString();
    }else{
      return num.toString().substring(0,num.toString().lastIndexOf(".")+postion+1).toString();
    }
  }

  static String savedTwoDecimal(double? price, bool isSaveZero) {
    if (price == null) {
      if (isSaveZero) {
        return '0.00';
      } else {
        return '0';
      }
    }
    NumberFormat format = new NumberFormat("0.##");
    if (isSaveZero) {
      format = new NumberFormat("0.00");
    }
    return format.format(price);
  }

  static double savedTwoDecimalForDouble(double? price, bool isSaveZero) {
    if (price == null) {
      if (isSaveZero) {
        return 0.00;
      } else {
        return 0;
      }
    }
    NumberFormat format = new NumberFormat("0.##");
    if (isSaveZero) {
      format = new NumberFormat("0.00");
    }
    return double.parse(format.format(price));
  }

  //获取两位小数 .0 或者.00则去掉
  static String getTwoDecimal(num? price) {
    if (price == null) {
      return '0';
    }
    if (price == 0) {
      return '0';
    }
    var fixed = price.toStringAsFixed(2);
    if (fixed.endsWith('.0') || fixed.endsWith('.00')) {
      return price.toStringAsFixed(0);
    }
    return fixed;
  }

  //获取两位小数 .0 或者.00则去掉 ；.x0去掉0
  static dynamic getTwoDecimalForDynamic(num? price) {
    if (price == null) {
      return '0';
    }
    if (price == 0) {
      return '0';
    }
    var fixed = price.toStringAsFixed(2);

    if (fixed.endsWith('.0') || fixed.endsWith('.00')) {
      return price.toStringAsFixed(0);
    }
    if (fixed.endsWith('0')) {
      return price.toStringAsFixed(1);
    }

    return fixed;
  }

  //四舍五入
  static String getRound(double price) {
    NumberFormat format = new NumberFormat("#");
    return format.format(price);
  }
}
